package com.xiaoxi.dragsortlistview.demo5;

import android.app.DialogFragment;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.xiaoxi.dragsortlistview.R;

import java.util.Arrays;
import java.util.List;

/**
 * listView单选的一种实现方法
 * 以及DialogFragment使用
 * @author libenqi@pptv.com
 * @version [版本号, 2018/11/30]
 * @see [相关类/方法]
 * @since [产品/模块版本]
 */
public class CancelOrderDialog extends DialogFragment
{
    private ListView mListView;
    private Button mConfirm;
    private Button mCancel;
    DialogAdapter dialogAdapter;
    /**
     * 对外部提供创建本对象的实例
     *
     * @return
     */
    public static CancelOrderDialog newInstance() {
        CancelOrderDialog dialogFragment = new CancelOrderDialog();
        return dialogFragment;
    }
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);

    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState)
    {
        getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE);
        View view = View.inflate(getActivity(),  R.layout.dialog_cancel_order,null);
        Window window = getDialog().getWindow();
        if (null != window) {
            window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        }
        mListView = (ListView) view.findViewById(R.id.lv_im_order_dialog_list);
        mConfirm = (Button) view.findViewById(R.id.btn_im_dialog_confirm);
        mConfirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                int pos= dialogAdapter.getPosition();
//                CancelOrderObservableEvent.getInstance().onCancel(pos+1);
                dismiss();
            }
        });
        mCancel = (Button) view.findViewById(R.id.btn_im_dialog_cancel);
        mCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
        String[] strings = new String[]{"暂时不方便服务", "已与用户协商取消", "待确认信息"};
        dialogAdapter = new DialogAdapter(getActivity(), Arrays.asList(strings));
        mListView.setAdapter(dialogAdapter);
        mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                dialogAdapter.setPosition(position);

//                LogUtil.e("bench","-=pos=="+ mListView.getSelectedItemPosition());
//                LogUtil.e("bench","-dialogAdapter=pos=="+ dialogAdapter.getPosition());
            }
        });
        return view;
    }

    private class ViewClick implements View.OnClickListener {

        @Override
        public void onClick(View v) {
            dismiss();
        }
    }

    public static class DialogAdapter extends BaseAdapter
    {


        private Context mContext;
        private List mList;

        public int getPosition() {
            return mPosition;
        }

        public void setPosition(int cPosition) {
            mPosition = cPosition;
            notifyDataSetChanged();
        }

        private int mPosition=0;

        public DialogAdapter(Context cContext, List cList) {
            mContext = cContext;
            mList = cList;
        }

        @Override
        public int getCount() {
            return 3;
        }

        @Override
        public Object getItem(int position) {
            return null;
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {

            convertView = View.inflate(mContext, R.layout.item_dialog_cancel_cell, null);
            final TextView textView = (TextView) convertView.findViewById(R.id.tv_im_cancel_content);
            final ImageView imageView = (ImageView) convertView.findViewById(R.id.iv_im_dialog_check);
            textView.setText(mList.get(position) + "");
            if (mPosition==position){
                textView.setTextColor(mContext.getResources().getColor(R.color.business_red_color));
                imageView.setVisibility(View.VISIBLE);
            }else {
                textView.setTextColor(mContext.getResources().getColor(R.color.color_666666));
                imageView.setVisibility(View.GONE);
            }

            return convertView;
        }
    }

}
